package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.domain.VideoAuth;
import com.xmg.p2p.base.mapper.VideoAuthMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.VideoAuthQueryObject;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.service.IVideoAuthService;
import com.xmg.p2p.base.util.BitStatesUtils;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * lcd  2020/5/19
 * Description:
 */
@Service
public class VideoAuthServiceImpl implements IVideoAuthService {
    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
    private IUserinfoService userinfoService;

    @Override
    public int insert(VideoAuth record) {
        return videoAuthMapper.insert(record);
    }

    @Override
    public VideoAuth selectByPrimaryKey(Long id) {
        return videoAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult queryPage(VideoAuthQueryObject qo) {
        int i = videoAuthMapper.queryForCount(qo);
        if (i > 0) {
            return new PageResult(videoAuthMapper.queryForPage(qo), i, qo.getCurrentPage(), qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public void audit(Long loginInfoId, String remark, int state) {
        Userinfo userinfo = userinfoService.findById(loginInfoId);
        if (userinfo!=null && !userinfo.getIsVedioAuth()) {
            VideoAuth videoAuth = new VideoAuth();
            videoAuth.setRemark(remark);
            videoAuth.setState(state);
            //审核人员
            videoAuth.setAuditTime(new Date());
            Logininfo auditor = new Logininfo();
            auditor.setId(UserContext.getLogininfo().getId());
            videoAuth.setAuditor(auditor);
            //申请人员
            Logininfo applyer = new Logininfo();
            applyer.setId(loginInfoId);//传进来的是申请的前台过来的用户
            videoAuth.setApplier(applyer);
            videoAuth.setApplyTime(new Date());
            //保存审核对象
            videoAuthMapper.insert(videoAuth);
            //审核通过,修改用户状态码
            if (state == VideoAuth.STATE_AUDIT) {
                userinfo.addStatue(BitStatesUtils.OP_VEDIO_AUTH);
                userinfoService.update(userinfo);
            }
        }
    }
}
